package com.bjc.base.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Component;

@Component
// @Order(1)
// @WebFilter(urlPatterns = { "/api/v/*" }, filterName = "tokenAuthorFilter")
public class MyHttpServletRequestFilter implements Filter {

	@Override
	public void destroy() {
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		// 在这里给FilterChain
		// 传递的参数是传装饰后的request,之后调用的就是EscapeWrapper重写的getParameter(String
		// name)这个方法
		// 获取装饰后的Request对象
		HttpServletRequest newRequest = new MyHttpServletRequestWrapper((HttpServletRequest) request);
		// 使用过滤器就是在请求到到server之前，调用service(HttpServletRequest
		// request,HttpServletResponse response)之前将request对象替换为装饰后的request对象
		// 只要使用了这个过滤器的请求的reques对象都会被替换为装饰后的request 对象，这样之后调用的getParameter(String
		// str)方法自然就是装饰后的类的方法

		chain.doFilter(newRequest, response);

	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {
	}
}
